package com.yanboo.modules.mnt.rest;

import com.yanboo.common.annotation.CustomController;
import com.yanboo.modules.mnt.service.AppService;
import com.yanboo.modules.mnt.service.dto.AppDto;
import com.yanboo.modules.mnt.service.dto.AppQueryCriteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import com.yanboo.logging.annotation.Log;
import com.yanboo.modules.mnt.domain.App;
import com.yanboo.common.utils.PageResult;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Set;

/**
 * 运维：应用管理
 */
@Tag(name = "运维：应用管理")
@CustomController
@RequiredArgsConstructor
@RequestMapping("/api/app")
public class AppController {

    private final AppService appService;

    /**
     * 导出应用数据
     * @param response
     * @param criteria
     * @throws IOException
     */
    @Operation(summary = "导出应用数据")
    @GetMapping(value = "/download")
    public void exportApp(HttpServletResponse response, AppQueryCriteria criteria) throws IOException {
        appService.download(appService.queryAll(criteria), response);
    }

    /**
     * 查询应用
     * @param criteria
     * @param pageable
     * @return
     */
    @Operation(summary = "查询应用")
    @GetMapping
    public ResponseEntity<PageResult<AppDto>> queryApp(AppQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(appService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    /**
     * 新增应用
     * @param resources
     * @return
     */
    @Operation(summary = "新增应用")
    @Log("新增应用")
    @PostMapping
    public ResponseEntity<Object> createApp(@Validated @RequestBody App resources){
        appService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    /**
     * 修改应用
     * @param resources
     * @return
     */
    @Operation(summary = "修改应用")
    @Log("修改应用")
    @PutMapping
    public ResponseEntity<Object> updateApp(@Validated @RequestBody App resources){
        appService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除应用
     * @param ids
     * @return
     */
    @Operation(summary = "删除应用")
    @Log("删除应用")
	@DeleteMapping
    public ResponseEntity<Object> deleteApp(@RequestBody Set<Long> ids){
        appService.delete(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
